/**
* Abstract file upload to a controller base
*/
package com.godpaper.as9.control.upload
{
	import com.godpaper.as9.constants.UploadInfoList;
	import com.godpaper.as9.util.LogHelper;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import mx.controls.Alert;
	/**
	 * 
	 * @author YangBo
	 * 
	 */
	public class FileUploadControllerBase extends EventDispatcher implements IFileUploadControllerBase
	{
		private var _urlRequest:URLRequest;
		private var _urlVariables:URLVariables;
		private var _maxFileSize:Number;
		private var _uploadTypes:Array;
//--------------------------------------------------------------------------
//
//  Construct
//
//--------------------------------------------------------------------------		
		public function FileUploadControllerBase(
									   method:String="POST",
									   contentType:String="multipart/form-data",
									   target:IEventDispatcher=null)
		{
			super(target);
			
			_urlRequest = new URLRequest();
			_urlVariables = new URLVariables();
			_maxFileSize = UploadInfoList.UPLOAD_MAX_FILE_SIZE;
			_uploadTypes = [UploadInfoList.TYPES_IMAGE];
			
			_urlRequest.data = _urlVariables;
			_urlRequest.method = method;
			_urlRequest.contentType = contentType;
			
		}
//--------------------------------------------------------------------------
//
//  Properties
//
//--------------------------------------------------------------------------
		//----------------------------------
		//  urlRequest
		//----------------------------------	
		public function set urlRequest(value:URLRequest):void
		{
			_urlRequest = value;
		}		
		public function get urlRequest():URLRequest
		{
			return _urlRequest;
		}
		//----------------------------------
		//  urlVariables
		//----------------------------------
		public function set urlVariables(value:URLVariables):void
		{
			_urlVariables = value;
			urlRequest.data = value;
		}
		[Bindable]
		public function get urlVariables():URLVariables
		{
			return _urlVariables;
		}
		//----------------------------------
		//  maxFileSize
		//----------------------------------
		public function set maxFileSize(value:Number):void
		{
			_maxFileSize = value;
		}
		public function get maxFileSize():Number
		{
			return _maxFileSize;
		}
		//----------------------------------
		//  uploadTypes
		//----------------------------------
		public function set uploadTypes(value:Array):void
		{
			_uploadTypes = value;
		}
		public function get uploadTypes():Array
		{
			return _uploadTypes;
		}
		//----------------------------------
		//  configureListeners
		//----------------------------------
		public function configureListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.CANCEL, cancelHandler);
            dispatcher.addEventListener(Event.COMPLETE, uploadCompleteHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(Event.SELECT, selectHandler);
            dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadDataCompleteHandler);
	    }
		
		public function browseHandler(event:Event):void
		{
		}
		
		public function openHandler(event:Event):void
		{
			LogHelper.debug("openHandler: " + event);	
		}
		
		public function cancelHandler(event:Event):void
		{
			LogHelper.debug("cancelHandler: " + event);
		}
		
		public function selectHandler(event:Event):void
		{
		}
		
		public function progressHandler(event:ProgressEvent):void
		{
		}
		
		public function ioErrorHandler(event:IOErrorEvent):void
		{
			LogHelper.debug("ioErrorHandler: " + event);
		}
		
		public function httpStatusHandler(event:HTTPStatusEvent):void
		{
			LogHelper.debug("httpStatusHandler: " + event);
			switch(event.status.toString()){
        		case "200":
        		case "401":
        		case "500":
        			Alert.show("upload.httpstatus.error");
        			break;
        		default:
        			Alert.show("upload.httpStatusError");
        			break;	
        	}          
		}
		
		public function uploadCompleteHandler(event:Event):void
		{
			LogHelper.debug("completeHandler: " + event);
		}
		
		public function uploadDataCompleteHandler(event:DataEvent):void
		{
		}
		public function securityErrorHandler(event:SecurityErrorEvent):void
		{
			LogHelper.debug("securityErrorHandler: "+ event);
		}
		
		public function updateUIElements(currentProgress:Number,
											progressBarLabel:String,
											buttonLabel:String,
											totalProgress:Number=100,
											progressBarVisible:Boolean=true,
											browseButtonEnable:Boolean=true,
											displayEnabled:Boolean=true):void
        {
        }
        
        // Checks the files do not exceed maxFileSize | if maxFileSize == 0 No File Limit Set
        protected function checkFileSize(filesize:Number):Boolean
        {
        	var r:Boolean = false;
    		//if  filesize greater then maxFileSize
	        if (filesize > maxFileSize){
	        	r = false;
	       	}else if (filesize <= maxFileSize){
	        	r = true;
        	}
        	
        	if (UploadInfoList.UPLOAD_MAX_FILE_NUM == 0){
        		r = true;
        	}
	   	
        	return r;
        }
		
	}
}